from datetime import datetime
from pydantic import BaseModel, Field
from typing import Optional, Any, List


class UserExam(BaseModel, extra="ignore"):
    userExamId: str
    userId: int
    examId: str
    startTime: datetime
    submitTime: Optional[datetime] = None
    totalScore: int
    status: str
    score: Optional[float] = None
    totalQuestions: int
    correctCount: Optional[int] = None
    totalTime: Optional[int] = None
    maxTime: Optional[int] = None
    passScore: int
    duration: int
    questions: Optional[Any] = None


class UserAnswer(BaseModel, extra="ignore"):
    recordId: str
    userId: int
    userExamId: str
    questionId: str
    questionType: str
    answerContent: str
    answerTime: str
    stayTime: int
    isCorrect: int
    scoreEarned: int
    questionOrder: Optional[int] = None
    questionScore: Optional[int] = None


class Option(BaseModel):
    optionId: str
    questionId: str
    optionContent: str
    optionOrder: Optional[int] = None  # 允许为 None
    label: str


class Question(BaseModel):
    questionId: str
    title: str
    questionType: str
    difficulty: int
    category: str
    status: int
    score: int
    explanation: str
    answer: str
    options: list[Option]
    userAnswer: Optional[UserAnswer] = None
    questionOrder: int
    questionScore: int


class ExamDetail(BaseModel):
    userExamId: str
    userId: int
    examId: str
    startTime: datetime
    submitTime: Optional[datetime] = None
    totalScore: int
    status: str
    score: Optional[float] = None
    totalQuestions: int
    correctCount: Optional[int] = None
    totalTime: Optional[int] = None
    maxTime: Optional[int] = None
    passScore: int
    duration: int
    questions: list[Question]


# 新增：前端数据格式 BaseModel 定义

class TypeDataItem(BaseModel):
    """题型数据项"""
    name: str = Field(description="题型名称，如：单选题、多选题")
    total: int = Field(description="该题型总题数")
    correct: int = Field(description="该题型答对题数")
    accuracy: str = Field(description="该题型正确率，如：90%")


class AbilityDataItem(BaseModel):
    """能力数据项"""
    name: str = Field(description="能力维度名称，如：记忆力、理解力")
    score: int = Field(description="该能力得分，0-100")
    describe: str = Field(description="该能力的描述，如：记忆力较强，能快速记住信息")


class ItemContent(BaseModel):
    """题目内容数据项"""
    type: str = Field(description="题目类型，如：text、image")
    content: str = Field(description="题目内容")


class WrongQuestionItem(BaseModel):
    """错题数据项"""
    title: str | list[ItemContent] = Field(description="题目标题")
    answer: str = Field(description="正确答案")
    userAnswer: str = Field(description="用户答案")
    explanation: str = Field(description="正对题目与回答做出解析，不能照搬答案回复")


class ParsedExamResult(BaseModel):
    """解析后的考试结果数据格式"""
    # 基本信息
    score: float = Field(description="实际得分")
    totalScore: int = Field(description="总分")
    scorePercent: float = Field(description="得分百分比")
    rank: str = Field(description="排名，如：前10%")
    
    # 答题概览
    totalQuestions: int = Field(description="题目总数")
    correctCount: int = Field(description="答对题数")
    wrongCount: int = Field(description="答错题数")
    accuracy: str = Field(description="正确率，如：85%")
    
    # 时间分析
    totalTime: int = Field(description="总用时（秒）")
    avgTime: int = Field(description="平均每题用时（秒）")
    maxTime: int = Field(description="最长用时（秒）")
    maxTimeQuestion: int = Field(description="用时最长的题目序号")
    timeData: List[int] = Field(description="每题用时数组（秒）")
    
    # 题型分析
    typeData: List[TypeDataItem] = Field(description="题型分析数据")
    
    # 能力分析
    abilityData: List[AbilityDataItem] = Field(description="能力分析数据")
    
    # AI建议
    aiSuggestions: List[str] = Field(description="AI建议列表与推荐书籍")
    aiSummary: str = Field(description="AI总结")
    
    # 错题分析
    wrongQuestions: List[WrongQuestionItem] = Field(description="错题列表")
